import 'package:enter_prise_web/iconfont/icon_font.dart';
import 'package:enter_prise_web/page/index/index_controller.dart';
import 'package:enter_prise_web/page/work/work_controller.dart';
import 'package:enter_prise_web/page/work/work_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class FilterView extends StatefulWidget {
  const FilterView(
      {super.key, required this.indexController, required this.callBack});
  final IndexController indexController;
  final Function(FilterType selectType) callBack;
  @override
  State<FilterView> createState() => _FilterViewState();
}

class _FilterViewState extends State<FilterView>
    with SingleTickerProviderStateMixin {
  late int selectIndex;
  late AnimationController animationController;
  late Animation<double> _animation;
  late CurvedAnimation _curvedAnimation;
  final WorkController workController = Get.find();

  double top = -500;

  @override
  void initState() {
    super.initState();
    selectIndex = widget.indexController.filterIndex;
    animationController = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 500));
    _curvedAnimation = CurvedAnimation(
        parent: animationController, curve: Curves.fastOutSlowIn);
    _animation = Tween(begin: -500.0, end: 0.0).animate(_curvedAnimation)
      ..addListener(() {
        setState(() {
          top = _animation.value;
        });
      })
      ..addStatusListener((staus) {
        if (staus == AnimationStatus.dismissed) {
          widget.indexController.makeFilterIsNotShow();
        }
      });

    animationController.forward();
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        if (animationController.isAnimating) return;
        animationController.reverse();
        FilterType? filterType;
        switch (selectIndex) {
          case 0:
            filterType = FilterType.day;
            break;
          case 2:
            filterType = FilterType.month;
            break;
          case 3:
            filterType = FilterType.year;
            break;
          case 4:
            filterType = FilterType.illegal;
            break;
          default:
            filterType = FilterType.week;
            break;
        }

        widget.callBack(filterType);
      },
      child: Container(
        color: Colors.black45.withOpacity(0.3),
        child: Stack(
          children: [
            Positioned(
              top: top,
              left: 0,
              right: 0,
              child: _renderFilterView(selectIndex, callBack: (index, name) {
                setState(() {
                  selectIndex = index;
                  widget.indexController.filterText.value = name;
                  widget.indexController.filterIndex = index;
                });
              }),
            )
          ],
        ),
      ),
    );
  }

  Widget _renderFilterView(int selectIndex,
      {required Function(int index, String name) callBack}) {
    List<FilterObj> filters = [
      FilterObj(
        "过去一天",
        IconFont(
          IconNames.clown,
          size: 25,
        ),
      ),
      FilterObj(
          "过去一周",
          IconFont(
            IconNames.afro,
            size: 25,
          )),
      FilterObj(
          "过去一月",
          IconFont(
            IconNames.businessman,
            size: 25,
          )),
      FilterObj(
          "过去一年",
          IconFont(
            IconNames.flirt,
            size: 25,
          )),
      FilterObj(
          "未审核",
          IconFont(
            IconNames.clown,
            size: 25,
          )),
    ];
    List<Widget> widgets = [];
    filters.asMap().forEach(
      (key, value) {
        widgets.add(GestureDetector(
          onTap: () {
            callBack(key, filters[key].name);
          },
          child: Container(
            color: Colors.white,
            height: 50,
            child: Stack(
              children: [
                key < filters.length - 1
                    ? Positioned(
                        left: 0,
                        right: 0,
                        bottom: 0,
                        child: Container(
                          height: 1,
                          color: const Color.fromRGBO(230, 230, 230, 1),
                        ),
                      )
                    : Container(),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    filters[key].icon,
                    Container(
                        margin: const EdgeInsets.only(left: 10),
                        child: Text(value.name,
                            style: const TextStyle(fontSize: 16))),
                    Expanded(
                      child: Container(),
                    ),
                    key == selectIndex
                        ? Padding(
                            padding:
                                const EdgeInsets.symmetric(horizontal: 8.0),
                            child: IconFont(
                              IconNames.duigou_cu,
                              size: 30,
                            ))
                        : const SizedBox.shrink()
                  ],
                ),
              ],
            ),
          ),
        ));
      },
    );
    return Container(
      padding: EdgeInsets.only(
          top: MediaQuery.of(Get.context!).padding.top,
          left: 30,
          right: 30,
          bottom: 6),
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(10),
          bottomRight: Radius.circular(10),
        ),
      ),
      child: Column(
        children: widgets,
      ),
    );
  }
}

class FilterObj {
  final String name;
  final Widget icon;

  FilterObj(this.name, this.icon);
}
